import React from 'react';
import { Link } from 'dva/router';
import styles from './HistorySearch.less';
import { Icon } from 'antd-mobile';

let arr = [];

const storage = (item) => {
  const storage = localStorage.getItem(item) || '';

  arr = [];

  const pairs = storage.substring(1).split("&");

  for (const i in pairs) {
    if (pairs[i] === "") continue;
    const pair = pairs[i].split("=");
    const obj = {};
    obj[pair[0]] = pair[1];
    arr.push(
      <div key={i} className={styles.historyItem}>
        <Icon type={require('../../svg/time.svg')} className={styles.icon1} />
        <Link to={`/associationResult?id=${pair[1]}`} className={styles.name}>{pair[0]}</Link>
        <Icon type="cross" className={styles.icon2} onClick={del.bind(this, item, `&${pair[0]}=${pair[1]}`)} />
      </div>,
    );
    arr.reverse();
  }
};

let del = (item, str, e) => {
  let storage = localStorage.getItem(item) || '';
  storage = storage.replace(str, '');
  localStorage.setItem('association', storage);
  e.target.parentNode.parentNode.removeChild(e.target.parentNode);
};

const HistorySearch = (props) => {
  storage(props.item);

  return (
    <div>

      <div className={styles.head}>
        历史搜索
      </div>
      {arr}
    </div>
  );
};

export default HistorySearch;
